package com.spring.cloud.study.auth.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.spring.cloud.study.common.entity.SysMenu;
import com.spring.cloud.study.common.entity.SysRole;
import com.spring.cloud.study.common.entity.SysUser;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/3/1
 * Time: 11:00
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class SysSecurityUser implements UserDetails {

    private String username;
    private String passWord;
    private String displayName;
    private boolean enabled;
    private Set<SysRole> sysRoles;

    public SysSecurityUser() {

    }


    public SysSecurityUser(SysUser sysUser) {
        this.username = sysUser.getUsername();
        this.passWord = sysUser.getPassword();
        this.sysRoles = sysUser.getRoles();
        this.enabled = sysUser.isDisabled();
        this.displayName = sysUser.getDisplayName();
    }




    @Override
    @JsonIgnore
    public Set<SimpleGrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> authorityList = new HashSet<>();
        for (SysRole role : sysRoles) {
            if(role.getMenus() != null && role.getMenus().size() > 0){
                for (SysMenu menu : role.getMenus()){
                    if(menu.getType().equals("2"))
                        authorityList.add(new SimpleGrantedAuthority(menu.getPerms()));
                }

            }

        }
        return authorityList;
    }

    @Override
    @JsonIgnore
    public String getPassword() {
        return this.passWord;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return this.enabled;
    }

    public void setUsername(String userName) {
        this.username = userName;
    }


    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public Set<SysRole> getSysRoles() {
        return sysRoles;
    }

    public void setSysRoles(Set<SysRole> sysRoles) {
        this.sysRoles = sysRoles;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }


    @Override
    public String toString() {
        return "SysSecurityUser{" +
                "userName='" + username + '\'' +
                ", passWord='" + passWord + '\'' +
                ", enabled=" + enabled +
                ", sysRoles=" + sysRoles +
                '}';
    }
}
